﻿using System;
using System.Collections.Generic;
using System.Text;
using GraffitiExtras.CommunityCreditWebservice;
using System.Data;

namespace GraffitiExtras.Plugins.CommunityCredit
{
    internal class ServiceClient
    {
        #region Properties and Fields

        private AffiliateServices _service = new AffiliateServices();
        private string _affiliateCode = string.Empty;
        private string _affiliateKey = string.Empty;

        #endregion

        #region Constructors

        public ServiceClient(string affiliateCode, string affiliateKey)
        {
            InitializeService();

            this._affiliateCode = affiliateCode;
            this._affiliateKey = affiliateKey;
        }

        #endregion

        #region Public methods

        public string AddCommunityCredit(string earnerEmail, string earnerFirstName,
            string earnerLastName, string taskDescription, string referenceURL, string pointCategory)
        {
            return this._service.AddCommunityCredit(earnerEmail, earnerFirstName, earnerLastName,
                taskDescription, referenceURL, pointCategory, this._affiliateCode, this._affiliateKey);
        }

        public string AddCommunityCreditByDate(string earnerEmail, string earnerFirstName,
            string earnerLastName, string taskDescription, string referenceURL,
            string pointCategory, DateTime dateEarned)
        {
            return this._service.AddCommunityCreditByDate(earnerEmail, earnerFirstName, earnerLastName,
                taskDescription, referenceURL, pointCategory, dateEarned, this._affiliateCode, this._affiliateKey);
        }

        public string AddNewsgroupCredit(string earnerEmail, string earnerFirstName,
            string earnerLastName, string taskDescription, string referenceURL,
            string pointCategory, DateTime dateEarned)
        {
            return this._service.AddNewsgroupCredit(earnerEmail, earnerFirstName, earnerLastName,
                taskDescription, referenceURL, pointCategory, dateEarned, this._affiliateCode, this._affiliateKey);
        }

        public string AddNewsgroupItem(string messageId, string referenceId, string newsgroupID,
            string user, string subject, string message, string newsgroupDate)
        {
            return this._service.AddNewsgroupItem(messageId, referenceId, newsgroupID, user,
                subject, message, newsgroupDate, this._affiliateCode, this._affiliateKey);
        }

        public string AutoSubmitBlog(string feedURL, string blogURL, string blogTitle, DateTime blogDate)
        {
            return this._service.AutoSubmitBlog(feedURL, this._affiliateCode, this._affiliateKey,
                blogURL, blogTitle, blogDate);
        }

        public DataSet CurrentContestPrizes()
        {
            return this._service.CurrentContestPrizes(this._affiliateCode, this._affiliateKey);
        }

        public string GenerateFutureCredit(string earnerEmail, string earnerFirstName, string earnerLastName,
            string taskDescription, string referenceURL, string pointCategory)
        {
            return this._service.GenerateFutureCredit(earnerEmail, earnerFirstName, earnerLastName,
                taskDescription, referenceURL, pointCategory, this._affiliateCode, this._affiliateKey);
        }

        public DataSet GetDemographics()
        {
            return this._service.GetDemographics(this._affiliateCode, this._affiliateKey);
        }

        public DataSet GetEarnersByDateRangeAndEmail(string email, string startDate, string endDate)
        {
            return this._service.GetEarnersByDateRangeAndEmail(email, startDate, endDate,
                this._affiliateCode, this._affiliateKey);
        }

        public DataSet GetTopEarners()
        {
            return this._service.GetTopEarners(this._affiliateCode, this._affiliateKey);
        }

        public DataSet GetTopEarnersByDateRange(string startDate, string endDate)
        {
            return this._service.GetTopEarnersByDateRange(startDate, endDate,
                this._affiliateCode, this._affiliateKey);
        }

        public DataSet PointCategoriesAll()
        {
            return this._service.PointCategoriesAll(this._affiliateCode, this._affiliateKey);
        }

        public void SendConfirmationEmail(string emailAddress, string firstName)
        {
            this._service.SendConfirmationEmail(emailAddress, firstName, this._affiliateCode);
        }

        public void SendWelcomeEmail(string source, string strTo, string firstName, string strPassword,
            string strCommunities, string strPasswordType)
        {
            this._service.SendWelcomeEmail(source, strTo, firstName,
                strPassword, strCommunities, strPasswordType);
        }

        public string SpreadTheWord(string fromName, string toEmailAddress, string firstName, string lastName)
        {
            return this._service.SpreadTheWord(this._affiliateCode, this._affiliateKey,
                fromName, toEmailAddress, firstName, lastName);
        }

        #endregion

        #region Private methods

        private void InitializeService()
        {
            this._service.Timeout = 5000;
            this._service.UserAgent = "Community Credit Service Library for .NET (Ver 1.0) By Keyvan Nayyeri";
            this._service.AllowAutoRedirect = true;
        }

        #endregion
    }
}
